import { ref } from 'vue'
import { defineStore } from 'pinia';

// 存储聊天对象信息的pinia仓库
export const ChatUserInfoStore = defineStore('chatUserInfoStore', () => {

    class chatUser {
        constructor(avatarUrl, id, username) {
            this.avatarUrl = avatarUrl;
            this.id = id;
            this.username = username;
        }
    }

    const chatUserList = ref([])

    // 根据用户ID查找用户信息
    const getUserById = (userId) => {
        return chatUserList.value.find(user => user.id == userId);
    }

    // 添加或更新用户信息
    const addOrUpdateUser = (userInfo) => {
        const existingUserIndex = chatUserList.value.findIndex(user => user.id == userInfo.id);
        if (existingUserIndex !== -1) {
            chatUserList.value[existingUserIndex] = userInfo; // 更新已存在的用户信息
        } else {
            chatUserList.value.push(userInfo); // 添加新用户信息
        }
    }

    //向外暴露的变量或方法
    return {
        chatUserList,
        getUserById,
        addOrUpdateUser
    }
})